/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.control;

import com.compomics.dataprovider.control.logic.fasta.FastaSourceConverter;
import com.compomics.dataprovider.control.logic.fasta.UniprotFastaProvider;
import com.compomics.dataprovider.control.logic.taxonomy.UniprotTaxonomyProvider;
import com.compomics.dataprovider.model.fasta.FastaStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Kenneth
 */
public class CLIExecutor {

    File outputFile;
    HashMap<String, String> taxonomiesToDownload;
    List<String> filesToMerge;
    boolean isReviewOnly;
    boolean isDecoy;
    boolean isAppend;
    boolean isCrap;
    boolean is_rem_dup_seq;
    boolean is_rem_dup_acc;

    public CLIExecutor() {

    }

    /**
     *
     * @return boolean indicating if the processing worked
     * @throws IOException
     * @throws URISyntaxException
     */
    public boolean execute() throws IOException, URISyntaxException {
        FastaStream stream = new FastaStream(FastaSourceConverter.convertToStream(outputFile));
        //first do the local concatenation
       StringBuilder report = new StringBuilder();
        if (filesToMerge != null) {
            for (String aFile : filesToMerge) {
                stream.concatenate(new File(aFile));
            }
        }
        //merge the URLs
        if (taxonomiesToDownload != null) {
            for (Map.Entry<String, String> aTaxonomy : taxonomiesToDownload.entrySet()) {
                UniprotFastaProvider.downloadFastaToStream(stream, aTaxonomy.getKey(), aTaxonomy.getValue(), isReviewOnly);
            }
        }
        //remove duplicats
        if (is_rem_dup_seq) {
            stream.removeDuplicateSequences();
        }
        if (is_rem_dup_acc) {
            stream.removeDuplicateAccessions();
        }
        if (isCrap) {
            stream.appendCRAP();
        }
        if (isDecoy) {
            stream.addDecoys();
        }
        stream.save(outputFile, isAppend);
        return true;
    }

    /**
     *
     * @return the outputfile
     */
    public File getOutputFile() {
        return outputFile;
    }

    /**
     *
     * @param outputFile
     * @sets the outputfile
     */
    public void setOutputFile(File outputFile) {
        this.outputFile = outputFile;
    }

    /**
     *
     * @return hashmap of the taxonomy ID's and names to download
     */
    public HashMap<String, String> getTaxonomiesToDownload() {
        return taxonomiesToDownload;
    }

    /**
     *
     * @param taxonomiesToDownload
     * @sets hashmap of the taxonomy ID's and names to download, starting from a already filled map
     */
    public void setTaxonomiesToDownload(HashMap<String, String> taxonomiesToDownload) {
        this.taxonomiesToDownload = taxonomiesToDownload;
    }
 /**
     *
     * @param taxonomiesToDownload
     * @sets hashmap of the taxonomy ID's and names to download, starting from a list of organism names (RECOMMENDED)
     */
    public void setTaxonomiesToDownload(List<String> organisms) {
        this.taxonomiesToDownload = new HashMap<>();
        UniprotTaxonomyProvider provider = new UniprotTaxonomyProvider();
        String taxonomyID;
        for (String anOrganism : organisms) {
            try {
                taxonomyID = provider.queryTaxonomy(anOrganism, true);
                taxonomiesToDownload.put(anOrganism.replace("+", " "), taxonomyID);
            } catch (IOException ex) {
                System.out.println("Could not find " + anOrganism + " : ");
                ex.printStackTrace();
            }

        }
    }

    /**
     *
     * @return the inputfiles
     */
    public List<String> getFilesToMerge() {
        return filesToMerge;
    }

    /**
     *
     * @param filesToMerge
     */
    public void setFilesToMerge(List<String> filesToMerge) {
        this.filesToMerge = filesToMerge;
    }

    /**
     *
     * @return boolean if non-reviewed sequences have to be included
     */
    public boolean isReviewedOnly() {
        return isReviewOnly;
    }

    public void setIsReviewOnly(boolean isReviewOnly) {
        this.isReviewOnly = isReviewOnly;
    }

    public boolean isDecoy() {
        return isDecoy;
    }

    public void setIsDecoy(boolean isDecoy) {
        this.isDecoy = isDecoy;
    }

    public boolean isAppend() {
        return isAppend;
    }

    public void setIsAppend(boolean isAppend) {
        this.isAppend = isAppend;
    }

    public boolean is_rem_dup_seq() {
        return is_rem_dup_seq;
    }

    public void setDeleteDuplicateSequences(boolean is_rem_dup_seq) {
        this.is_rem_dup_seq = is_rem_dup_seq;
    }

    public boolean is_rem_dup_acc() {
        return is_rem_dup_acc;
    }

    public void setDeleteDuplicateAccessions(boolean is_rem_dup_acc) {
        this.is_rem_dup_acc = is_rem_dup_acc;
    }

    public void setIsCrap(boolean isCrap) {
        this.isCrap = isCrap;
    }

    public boolean isCrap() {
        return isCrap;
    }

}
